<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

use app\admin\model\AuthRule as Menu;
use app\admin\model\AuthGroup;

class Auth extends Baseic
{
    public function __auto(){
      $this->model   = new Menu();
    }
    /**
     * get admin/Auth/index
     * 显示后台视图
     * @return view
     */
    public function index()
    {
      $data = $this->model->getAll();
      $this->assign('ruleList',$data);
      unset($data);
      return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
      if ($this->request->isPost()) {
         $data = input('post.');
         $re   = $this->model->addRule($data);
         if ($re) {
           $result = ['state'=>true,'msg'=>"新增成功"];
         }else {
           $result = ['state'=>false,'msg'=>"服务器繁忙"];
         }
         unset($data);
         unset($this->model);
         unset($re);
         return $result;
       }

       $data = $this->model->getAll();
       $this->assign('ruleList',$data);
       unset($data);
       unset($this->model);
       return view();
    }



    /**
     * 显示编辑    GET Auth/edit  @return view()
     * 保存编辑    POST Auth/edit @return json array();
     * 编辑菜单节点
     */
    public function edit()
    {
      $id = input('get.id');

      //POST Auth/edit 提交修改
      if ($this->request->isPost()) {
        $data = input('post.');
        $re   = $this->model->updateRule($data);
        if ($re) {
          $result = ['state'=>true,'msg'=>"成功"];
        }else {
          $result = ['state'=>false,'msg'=>"服务器繁忙"];
        }
        unset($data);
        unset($this->model);
        unset($re);

        return $result;
      }

      //GET Auth/edit 显示修改视图
      $rule = $this->model->getRule($id);
      $this->assign('rule',$rule);
      $data = $this->model->getAll();
      $this->assign('ruleList',$data);

      unset($rule);
      unset($data);

      return view();
    }

    /**
     * PUT: Auth/update
     * 保存更新的资源
     * @return \think\Response
     */
    public function update()
    {
      if ($this->request->isPut()) {
         $data = input('put.');

         $re   = $this->model->updateRule($data);

         if ($re) {
           $result = ['state'=>true,'msg'=>"成功"];
         }else {
           $result = ['state'=>false,'msg'=>"服务器繁忙"];
         }

         unset($data);
         unset($this->model);
         unset($re);

         return $result;
       }
    }

    /**
     * delete Auth/delete
     * 删除指定资源
     * @return json()
     */
    public function delete()
    {
      if ($this->request->isDelete()) {
       $data = input('delete.');
       $re   = $this->model->deleteRule($data['id']);
       if ($re) {
         $result = ['state'=>true,'msg'=>"成功"];
       }else {
         $result = ['state'=>false,'msg'=>"服务器繁忙"];
       }

       unset($data);
       unset($this->model);
       unset($re);

       return $result;
      }
    }



    /**
     *  get Auth/authgroup
     * 显示角色分组列表
     * @return view
     */
    public function authGroupRole(){
      $db   = new AuthGroup();
      $data = $db->getAllGroup();      //取得所有节点数据
      $this->assign('groupList',$data);
      unset($db);
      unset($data);
      return view();
    }

    /**
     * get/post Auth/createGroup
     * 创建角色分组
     * @param  array $data = input('post.')
     * @return json['state'=>false/true,'msg'=>""]
     */
    public function createGroup() {
      // post访问
      if ($this->request->isPost()) {
        $dump = '';
        $data = input('post.');
        $db   = new AuthGroup();
        //组合数据
        if (isset($data['rules'])) {
          foreach ($data['rules'] as $k => $v) {
            $dump .= $v.',';
          }
          $data['rules'] = $dump;
          if (sizeof($data['rules']) == $db->count()) {
            $data['is_admin'] = 1;
          }else {
            $data['is_admin'] = 0;
          }
        }
        // 插入数据库
        $re   = $db->addGroup($data);
        if ($re) {
          $result = ['state'=>true,'msg'=>"成功"];
        }else {
          $result = ['state'=>false,'msg'=>"服务器繁忙"];
        }

        unset($dump);
        unset($data);
        unset($db);
        unset($re);
        return $result;
      }

      //get 访问
      $data = $this->model->getAll();      //取得所有节点数据
      $this->assign('ruleList',$data);

      // p($data);
      unset($data);
      unset($this->model);
      return view();
    }

    /**
     * put Auth/editGroup
     * 编辑角色分组
     * @param  int  $id
     * @param  array $data = input('put.')
     * @return json['state'=>false/true,'msg'=>""]
     */
    public function editGroup(){
      $db   = new AuthGroup();
      // post访问
      if ($this->request->isPut()) {
        $dump = '';

        $data = input('put.');
        //组合数据
        if (isset($data['rules'])) {
          foreach ($data['rules'] as $k => $v) {
            $dump .= $v.',';
          }
          $data['rules'] = $dump;
          if (sizeof($data['rules']) == $db->count()) {
            $data['is_admin'] = 1;
          }else {
            $data['is_admin'] = 0;
          }
        }
        unset($data['id']);
        $id = input('put.id');

        // 判定 是否是修改当前管理者 ，如果是自己则不允许自己提升权限

        // 插入数据库
        $re  = $db->updateGroup($data,$id);
        if ($re) {
          $result = ['state'=>true,'msg'=>"成功"];
        }else {
          $result = ['state'=>false,'msg'=>"服务器繁忙"];
        }

        unset($dump);
        unset($data);
        unset($db);
        unset($re);
        return $result;
      }

      //get 访问
      $id   = input('get.id');
      $data = $this->model->getAll();      //取得所有节点数据
      $this->assign('ruleList',$data);

      $rule = $db->getGroup($id);      //取得指定ID节点数据
      $rule['rules'] = do_explode($rule['rules']);
      $this->assign('rule',$rule);

      unset($data);
      unset($db);
      unset($rule);
      unset($this->model);
      return view();
    }

    /**
     * delete Auth/deleteGroup
     * 删除角色分组
     * @param  int  $id or array $id   == input('delete.');
     * @return json['state'=>false/true,'msg'=>""]
     */
    public function deleteGroup(){
      if ($this->request->isDelete()) {
         $db   = new AuthGroup();
         if(!session('is_admin')){
           return ['state'=>false,'msg'=>"您不是最高权限用户,无法操作"];
         }

         $id  = input('delete.id');

         $re   = $db->deleteGroup($id);
         if ($re) {
           $result = ['state'=>true,'msg'=>"成功"];
         }else {
           $result = ['state'=>false,'msg'=>"服务器繁忙"];
         }

         unset($data);
         unset($db);
         unset($re);

         return $result;
       }
    }

    /*
      delete
      批量删除
      @return json
    */
    public function allDelete()
    {
       if ($this->request->isDelete()) {
         if(!session('is_admin')){
           return ['state'=>false,'msg'=>"您不是最高权限用户,无法操作"];
         }
         
         $uid  = input('delete.');
         $id = implode(',',$uid['id']);
         $obj = new AuthGroup();
         $res = $obj->deleteAll($id);
         if($res){
           $result = ['state'=>true,'msg'=>"成功"];
         }else{
           $result = ['state'=>false,'msg'=>"失败"];
         }
         return $result;
       }
    }
}
